;(function($){
	function Carousel(poster){
		var self = this;
		//保存单个旋转木马DOM对象
		this.poster 		 = poster;
		this.posterItemMain  = this.poster.find(".poster-list"); 
		this.posterBtn 		 = this.poster.find(".poster-btn");
		this.prevBtn		 = this.poster.find(".poster-prev-btn");
		this.nextBtn		 = this.poster.find(".poster-next-btn");
		this.posterItem 	 = this.posterItemMain.find("li.poster-item");
		if(this.posterItem.size()%2==0){
			this.posterItemMain.append(this.posterItem.eq(1).clone());
			this.posterItem=this.posterItemMain.children();
		}
		this.posterFirstItem = this.posterItem.first();
		this.posterLastItem  = this.posterItem.last();
		this.animateFlag	 = true;
		//默认配置参数
		this.settings = {
			"width":1000,
			"height":270,
			"posterWidth":640,
			"posterHeight":270,
			"scale":0.9,
			"speed":500,
			"autoplay":false,
			"delay":500,
			"verticalAlign":"middle"
		}
		//jquery拓展函数，手工配置覆盖默认配置
		$.extend(this.settings,this.getSettings())
		this.setCss()
		this.setPosterPos()
		this.prevBtn.click(function(){
			if(self.animateFlag===true){
				self.animateFlag=false;
				self.carouselRotate("right")
			}	
		})
		this.nextBtn.click(function(){
			if(self.animateFlag===true){
				self.animateFlag=false;
				self.carouselRotate("left")
			}
		})
		if(this.settings.autoplay){
			this.autoplay()
			this.poster.hover(function(){
				window.clearInterval(self.timer)
			},function(){
				self.autoplay()
			})
		}
	}
	Carousel.prototype={
		autoplay : function(){
			var self = this;
			this.timer 	= window.setInterval(function(){
				self.nextBtn.click()
			},this.settings.delay)
		},
		//左右旋转
		carouselRotate:function(dir){
			var that = this;
			var zIndexArr = [];
			if(dir === "left"){
				this.posterItem.each(function(){
					var prev    = $(this).prev().get(0)?$(this).prev():that.posterLastItem,
						width   = prev.width(),
						height  = prev.height(),
						left    = prev.css("left"),
						top     = prev.css("top"),
						opacity = prev.css("opacity");
						zIndexArr.push(prev.css("zIndex"));
					$(this).animate({
						width   : width,
						height  : height,
						left    : left,
						top     : top,
						opacity : opacity
					},that.settings.speed,function(){
						that.animateFlag=true
					})
				})
				this.posterItem.each(function(i){
    				$(this).css("zIndex",zIndexArr[i])
				})
			}else{
				this.posterItem.each(function(){
					var next    = $(this).next().get(0)?$(this).next():that.posterFirstItem,
						width   = next.width(),
						height  = next.height(),
						left    = next.css("left"),
						top     = next.css("top"),
						opacity = next.css("opacity");
						zIndexArr.push(next.css("zIndex"));
					$(this).animate({
						width   : width,
						height  : height,
						left    : left,
						top     : top,
						opacity : opacity
					},that.settings.speed,function(){
						that.animateFlag=true
					})
				})
				this.posterItem.each(function(i){
    				$(this).css("zIndex",zIndexArr[i])
				})
			}
		},
		//设置剩余帧的位置关系
		setPosterPos : function(){
			var self 		= this,
				posterSlice = this.posterItem.slice(1),
				sliceSize 	= posterSlice.size()/2,
				rightSlice 	= posterSlice.slice(0,sliceSize),
				leftSlice 	= posterSlice.slice(sliceSize,posterSlice.size()),
				level 		= Math.floor(this.posterItem.size()/2),
				rw 			= this.settings.posterWidth,
				rh 			= this.settings.posterHeight,
				gap			= ((this.settings.width-this.settings.posterWidth)/2)/level,
				offsetLeft  = rw+(this.settings.width-this.settings.posterWidth)/2;
			//右边帧的位置
			rightSlice.each(function(i){
				level--;
				rw = rw*self.settings.scale;
				rh = rh*self.settings.scale;
				var j = i;
				$(this).css({
					zIndex	: level,
					width   : rw,
					height  : rh,
					opacity : 1/(++i),
					left    : offsetLeft+gap*(++j)-rw,
					top     : self.verticalAlign(rh)
				})
			})
			//左边帧的位置
			var lw 		= rightSlice.last().width(),
				lh 		= rightSlice.last().height(),
				oloop 	= Math.floor(this.posterItem.size()/2);
			leftSlice.each(function(i){
				$(this).css({
					zIndex  : i,
					width   : lw,
					height  : lh,
					opacity : 1/oloop,
					left    : i*gap,
					top     : self.verticalAlign(lh)
				})
				oloop--;
				lw = lw/self.settings.scale;
				lh = lh/self.settings.scale;
			})	
		},
		//设置垂直样式
		verticalAlign : function(h){
				var verticalSet = this.settings.verticalAlign,
					top = 0;
				if(verticalSet==="top"){
					top = 0;
				}else if(verticalSet==="middle"){
					top = (this.settings.height-h)/2
				}else if(verticalSet==="bottom"){
					top = this.settings.height-h
				}else{
					top = (this.settings.height-h)/2
				}
				return top
		},
		//设置DOM CSS样式
		setCss : function(){
			this.poster.css({
				width : this.settings.width,
				height: this.settings.height,
			})
			this.posterItemMain.css({
				width  : this.settings.posterWidth,
				height : this.settings.posterHeight,
				left   : btnWidth 
			})
			var btnWidth  = (this.settings.width-this.settings.posterWidth)/2;
			var btnHeight = (this.settings.height);
			this.posterBtn.css({
				width:btnWidth,
				height:btnHeight,
				zIndex:Math.ceil(this.posterItem.size()/2),
				lineHeight:this.settings.height+"px"
			})
			this.posterFirstItem.css({
				width:this.settings.posterWidth,
				height:this.settings.posterHeight,
				left:btnWidth,
				zIndex:Math.floor(this.posterItem.size()/2)
			})
			
		},
		//获取人工配置参数
		getSettings : function(){
			var settings = this.poster.data("settings");
			return settings
		}
	}

	//初始化
	Carousel.init=function(posters){
		var __this__ = this;
		posters.each(function(){
			//实例化jquery dom 对象
			new __this__($(this))
		})
	}
	//全局注册
	window["Carousel"]=Carousel
})(jQuery)